package com.bangdream.michelia.view.fragment.exam;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bangdream.michelia.R;
import com.bangdream.michelia.application.AppCurrentUser;
import com.bangdream.michelia.contract.ExamContract;
import com.bangdream.michelia.entity.exam.AnswerBean;
import com.bangdream.michelia.entity.exam.ExamBean;
import com.bangdream.michelia.entity.exam.ExamQuestBean;
import com.bangdream.michelia.entity.exam.ExamResultDetailsBean;
import com.bangdream.michelia.entity.exam.ExamResultItemBean;
import com.bangdream.michelia.entity.exam.SubjectBean;
import com.bangdream.michelia.entity.exam.SubjectInfoBean;
import com.bangdream.michelia.presenter.ExamPresenter;
import com.bangdream.michelia.utils.L;
import com.bangdream.michelia.view.activity.exam.ExamHelper;
import com.bangdream.michelia.view.activity.exam.ExamResultActivity;
import com.bangdream.michelia.view.activity.exam.OnMainActivityListener;
import com.bangdream.michelia.view.custom.MyEditText;

import java.util.List;

/**
 * @date on 2018/8/18 19:22
 * @email 909732184@qq.com
 * 解答题
 */
public class SolveFragment extends BaseAnswerFragment<ExamContract.IExamView,ExamPresenter<ExamContract.IExamView>>
        implements View.OnClickListener,ExamContract.IExamView{
    private View view;
    private LinearLayout btnRemember;
    private ImageView ivRemember;
    //默认不标记
    private boolean isRemember = false;
    private TextView tvIndex,tvTotalIndex,tvTime;
    private int nmIndex,nmTotalIndex;
    private TextView tvTitle;
    private MyEditText editText;
    private SubjectBean subjectBean;
    private SubjectInfoBean data;
    //本次考试的考试id
    private String examId;
    private OnMainActivityListener onMainActivityListener;
    private boolean isNext;
    //试卷id
    private String paperId;
    //需要提交的答案
    private String stAnswer = "";
    @Override
    public ExamPresenter createPresenter(){
        return new ExamPresenter();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.solve_fragment,container,false);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
    }

    @Override
    public void loadData() {
        if(subjectBean!=null) {
            mPresenter.getSubjectDetails(subjectBean.getId(),"0",pd);
        }
        setListener();
    }


    private void initView() {
        if(getArguments()!=null){
            examId = getArguments().getString("examId");
            paperId = getArguments().getString("paperId");
            nmIndex = getArguments().getInt("index");
            nmTotalIndex = getArguments().getInt("totalIndex");
            subjectBean = getArguments().getParcelable("data");
        }
        tvTime = view.findViewById(R.id.tvTime);
        tvTitle = view.findViewById(R.id.tvTitle);
        editText = view.findViewById(R.id.editText);
        tvIndex = view.findViewById(R.id.tvIndex);
        tvTotalIndex = view.findViewById(R.id.tvTotalIndex);
        btnRemember = view.findViewById(R.id.btnRemember);
        ivRemember = view.findViewById(R.id.ivRemember);
        pd.setCanceledOnTouchOutside(false);
    }

    private void setViewData() {
        if(data==null)
            return;
        SubjectInfoBean.QuestionInfoListBean question = data.getQuestionInfoList();
        if(question==null)
            return;

        tvIndex.setText((nmIndex+1)+"");
        tvTotalIndex.setText("/"+nmTotalIndex+"");

        String stTitle = question.getDescription()+"";
        String stDescribe = "(解答题12分)";

        SpannableStringBuilder spanableBuilder = new SpannableStringBuilder(stTitle+" "+stDescribe);
        //取出描述的位置 改变颜色为蓝色
        int length = spanableBuilder.toString().indexOf(stDescribe);
        int end = length + stDescribe.length();
        //ForegroundColorSpan 为文字前景色，BackgroundColorSpan为文字背景色
        //如果要同时改变多处样式必须每次new一个对象
        spanableBuilder.setSpan(new ForegroundColorSpan(ContextCompat.getColor(getActivity(),R.color.holo_blue_light))
                , length, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        tvTitle.setText(spanableBuilder);
    }

    private void setListener() {
        btnRemember.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btnRemember:
                setRemember();
                break;
            default:break;
        }
    }

    private void setRemember() {
        isRemember = !isRemember;
        if(isRemember){
            ivRemember.setImageResource(R.mipmap.kaoshi_bj_answer_b);
        }else {
            ivRemember.setImageResource(R.mipmap.kaoshi_bj_answer_a);
        }
        ExamHelper.getInstance().SignSetSelect(nmIndex);
    }
    @Override
    public void setTime(String stTime) {
        tvTime.setText("剩余时间 "+stTime);
    }

    @Override
    public void saveAnswer(boolean isFinish,boolean isNext) {
        this.isNext = isNext;
        if(data==null||paperId==null||examId==null){
            if(isFinish){
                if(ExamHelper.getInstance().getAnswerBeans()!=null){
                    AnswerBean answerBean = new AnswerBean();
                    for (AnswerBean bean:ExamHelper.getInstance().getAnswerBeans()){
                        answerBean = bean;
                    }
                    answerBean.setIsFinish("1");
                    mPresenter.actionSubmitExam(answerBean,isFinish,pd);
                    return;
                }
            }
            showText("数据错误");
            getActivity().finish();
            return;
        }
        stAnswer = editText.getText().toString();
        L.d(stAnswer);
        //是否已经填写了答案
        if(stAnswer!=null&&stAnswer.length()>0) {
            ExamHelper.getInstance().getIsAnswerBegin().set(nmIndex,true);
        }else {
            ExamHelper.getInstance().getIsAnswerBegin().set(nmIndex,false);
        }
        submit(isFinish);
    }


    private void submit(boolean isFinish) {
        AnswerBean answerBean = new AnswerBean();
        answerBean.setAnswer(stAnswer);
        answerBean.setExamId(examId);
        //answerBean.setExamStart(stAnswerNos);
//        answerBean.setId(data.getId());
        answerBean.setId(subjectBean.getId());
        answerBean.setPaperId(paperId);
        SubjectInfoBean.QuestionInfoListBean question = data.getQuestionInfoList();
        if(question!=null)
            answerBean.setQuestionId(question.getId());
        answerBean.setUserId(AppCurrentUser.getInstance().getUserInfo().getId());
        //1：交卷 0：不交卷
        if(isFinish) {
            answerBean.setIsFinish("1");
        }else {
            answerBean.setIsFinish("0");
        }
        mPresenter.actionSubmitExam(answerBean,isFinish,pd);
        ExamHelper.getInstance().putAnswer(answerBean);
    }

    @Override
    public void setExamList(boolean isSuccess, int page, List<ExamBean> data, String stDescribe) {

    }

    @Override
    public void setSubjectList(boolean isSuccess, List<SubjectBean> data, String stDescribe) {

    }

    @Override
    public void setSubjectDetails(boolean isSuccess, SubjectInfoBean data, String stDescribe) {
        if(isSuccess){
            this.data = data;
            setViewData();
        }else {

        }
    }

    @Override
    public void initExamId(boolean isSuccess, String examId, String stDescribe) {

    }

    @Override
    public void actionSubmitExam(boolean isSuccess,boolean isFinish, String stDescribe) {
        if(isSuccess){
            if(isFinish){
                showText("提交成功");
                Intent intent = new Intent(getActivity(),ExamResultActivity.class);
                intent.putExtra("examId",  examId);
                startActivity(intent);
                getActivity().finish();
            }else {
                if(onMainActivityListener!=null){
                    if(isNext){
                        onMainActivityListener.onPageToNext();
                    }else {
                        onMainActivityListener.onPageToLast();
                    }
                }
            }
        }
    }

    @Override
    public void actionSubmitQuest(boolean isSuccess, boolean isFinish, String stDescribe) {

    }

    @Override
    public void setExamResultList(boolean isSuccess, List<ExamResultItemBean> data, String stDescribe) {

    }

    @Override
    public void setExamResultDetails(boolean isSuccess, ExamResultDetailsBean data, String stDescribe) {

    }

    @Override
    public void setExamQuestList(boolean isSuccess, List<ExamQuestBean> data, String stDescribe) {

    }

    @Override
    public void setExamQuList(boolean isSuccess, List<ExamBean> data, String stDescribe) {

    }

    @Override
    public void setOnMainActivityListener(OnMainActivityListener onMainActivityListener) {
        this.onMainActivityListener = onMainActivityListener;
    }
}
